package com.bravo.friend.repository;

/**
 * @author: Yuyong
 * @create: 2019-08-30 17:14
 **/

import com.bravo.friend.entity.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * 交友数据访问层
 */
public interface FriendRepository extends JpaRepository<Friend,String> {

    /**
     * 删除喜欢
     * @param userId
     * @param friendId
     */
    @Transactional
    @Modifying
    @Query(value = "delete from tb_friend f where f.user_id = ?1 and f.friend_id = ?2",nativeQuery = true)
    void deleteFriend(String userId,String friendId);

    /**
     * 根据用户ID与被关注用户ID查询记录个数
     * @param userId
     * @param friendId
     * @return
     */
    @Query(value = "select count(*) from tb_friend f where f.user_id=?1 and f.friend_id=?2",nativeQuery = true)
    int selectCount(String userId,String friendId);

    /**
     * 更新为互相喜欢
     * @param userId
     * @param friendId
     * @param isLike
     */
    @Transactional
    @Modifying
    @Query(value = "update tb_friend f set f.is_like=?3 where f.user_id=?1 and f.friend_id=?2",nativeQuery = true)
    void updateLike(String userId,String friendId,String isLike);

}
